﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;

namespace wallstreet
{
    public partial class Control_StockIndex : PhoneApplicationPage
    {
        public Control_StockIndex()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            //loadInfo();
        }

        public void loadInfo()
        {
            //^DJI
            markedDowJones.lblHeader.Text = "dow jones";
            markedDowJones.lblValue.Text = "";
            markedDowJones.lblChange.Text = "";

            //^GSPC
            markedSandP.lblHeader.Text = "s&p";
            markedSandP.lblValue.Text = "";
            markedSandP.lblChange.Text = "";

            //^IXIC
            markedNasdaq.lblHeader.Text = "nasdaq ";
            markedNasdaq.lblValue.Text = "";
            markedNasdaq.lblChange.Text = "";

            //^GDAXI
            markedDax.lblHeader.Text = "dax";
            markedDax.lblValue.Text = "";
            markedDax.lblChange.Text = "";

            //^FTSE
            markedFtse100.lblHeader.Text = "ftse 100";
            markedFtse100.lblValue.Text = "";
            markedFtse100.lblChange.Text = "";

            //^HSI
            markedHangSeng.lblHeader.Text = "hang seng";
            markedHangSeng.lblValue.Text = "";
            markedHangSeng.lblChange.Text = "";

            //^N225
            markedNikkei225.lblHeader.Text = "nikkei 225";
            markedNikkei225.lblValue.Text = "";
            markedNikkei225.lblChange.Text = "";

            //^SSEC
            markedShanghai.lblHeader.Text = "shanghai";
            markedShanghai.lblValue.Text = "";
            markedShanghai.lblChange.Text = "";

            //http://www.codeproject.com/KB/aspnet/StockQuote.aspx
            //http://download.finance.yahoo.com/d/quotes.csv?s=^DJI+^GSPC+^IXIC+^GDAXI+^FTSE+^OSEAX+^HSI+^N225&f=snl1p2
            //  s	=	Symbol
            //  n	=	Name
            //  l1	=	Last Trade (Price Only)
            //  p2	=	Change in Percent

            string url = "http://download.finance.yahoo.com/d/quotes.csv?s=" +
                                    "INDU+" +
                                    "^GSPC+" +
                                    "^IXIC+" +
                                    "^GDAXI+" +
                                    "^FTSE+" +
                                    "^HSI+" +
                                    "^N225+" +
                                    "^SSEC" + 
                                    "&f=snl1p2";

            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri(url));
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            using (StringReader reader = new StringReader(e.Result))
            {
                string stock;
                while ((stock = reader.ReadLine()) != null)
                {
                    if (stock.Contains("S&P 500 INDEX,RTH"))
                    {
                        stock = stock.Replace("S&P 500 INDEX,RTH", "S&P 500 INDEX - RTH");
                    }

                    string[] values = stock.Split(',');

                    values[0] = values[0].Replace("\"", "");
                    values[1] = values[1].Replace("\"", "");
                    values[2] = values[2].Replace("\"", "");
                    values[3] = values[3].Replace("\"", "");

                    SolidColorBrush color = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
                    string changeSymbol = "";

                    if (values[3].Substring(0, 1).Equals("-"))
                    {
                        changeSymbol = "▼ ";
                        color = new SolidColorBrush(Color.FromArgb(255, 190, 17, 17));
                    }
                    else
                    {
                        changeSymbol = "▲ ";
                        color = new SolidColorBrush(Color.FromArgb(255, 0, 186, 70));
                        //color = new SolidColorBrush(Color.FromArgb(255, 0, 0, 0));
                    }

                    switch (values[0])
                    {
                        case "^DJI":
                            markedDowJones.lblValue.Text = values[2];
                            markedDowJones.lblChange.Text = changeSymbol + values[3];
                            markedDowJones.lblChange.Foreground = color;
                            break;

                        case "^GSPC":
                            markedSandP.lblValue.Text = values[2];
                            markedSandP.lblChange.Text = changeSymbol + values[3];
                            markedSandP.lblChange.Foreground = color;
                            break;

                        case "^IXIC":
                            markedNasdaq.lblValue.Text = values[2];
                            markedNasdaq.lblChange.Text = changeSymbol + values[3];
                            markedNasdaq.lblChange.Foreground = color;
                            break;

                        case "^GDAXI":
                            markedDax.lblValue.Text = values[2];
                            markedDax.lblChange.Text = changeSymbol + values[3];
                            markedDax.lblChange.Foreground = color;
                            break;

                        case "^FTSE":
                            markedFtse100.lblValue.Text = values[2];
                            markedFtse100.lblChange.Text = changeSymbol + values[3];
                            markedFtse100.lblChange.Foreground = color;
                            break;

                        case "^HSI":
                            markedHangSeng.lblValue.Text = values[2];
                            markedHangSeng.lblChange.Text = changeSymbol + values[3];
                            markedHangSeng.lblChange.Foreground = color;
                            break;

                        case "^N225":
                            markedNikkei225.lblValue.Text = values[2];
                            markedNikkei225.lblChange.Text = changeSymbol + values[3];
                            markedNikkei225.lblChange.Foreground = color;
                            break;

                        case "000001.SS":
                            markedShanghai.lblValue.Text = values[2];
                            markedShanghai.lblChange.Text = changeSymbol + values[3];
                            markedShanghai.lblChange.Foreground = color;
                            break;
                    }

                }
            }
        }

    }
}